<?php

namespace App\Service\Question;

use App\Dao\Question\QuestionDao;
use App\Dao\Question\QuestionDetailDao;
use App\Model\Question\QuestionDetail;
use App\Service\Service;
use App\Tool\Collect;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class QuestionService extends Service
{
    /**
     * @Inject()
     * @var QuestionDao
     */
    protected $questionDao;

    /**
     * @Inject()
     * @var QuestionDetailDao
     */
    protected $questionDetailDao;

    public function lists($params)
    {
        return $this->questionDao->DaoWhere($params->toArray())->getList();
    }

    public function add($params)
    {
        Db::beginTransaction();
        try {
            $question = $this->questionDao->create($params->toArray());
            collect($params['question_detail'])->every(function ($detail) use ($question) {
                $res = $question->QuestionDetail()->save(new QuestionDetail($detail));
                return true;
            });
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollBack();
            _Error((string)$exception->getMessage());
            _Error($exception->getMessage());
        }
        return $question;
    }

    public function detail(int $questionId)
    {
        $data = $this->questionDao->DaoWhere(['id' => $questionId])->with(['QuestionDetail' => function ($query) {
            $query->select(['score', 'id', 'title', 'question_id', 'types']);
        }])->first();
        _SUCCESS($data);
    }

    public function edit($id, Collect $data)
    {
        $questionDao = $this->questionDao->DaoWhere(['id' => $id])->firstOr(function () {
            _Error('问题不存在');
        });
        $questionDao->fill($data->toArray());

        $questionIds = $questionDao->QuestionDetail->pluck('id');
        $questionDetail = collect($data['question_detail']);

        $questionDetail->where('id', '>', 0)->whereIn('id', $questionIds)->tap(function ($question) use ($questionDao) {
            $question->every(function ($item) use ($questionDao) {
                $questionDetailDao = $this->questionDetailDao->where('id', $item['id'])->firstOr(function () {
                    _Error('不存在');
                });
                $questionDetailDao->fill($item);
                $questionDetailDao->save();
                return true;
            });
        });

        $questionDetailIds = $questionDetail->where('id', '>', 0)->pluck('id')->toArray();
        collect($questionIds)->reject(function ($value) use ($questionDetailIds) {
            return in_array($value, $questionDetailIds);
        })->tap(function ($deleteIds) {
            $this->questionDetailDao->whereIn('id', $deleteIds->toArray())->delete();
        });

        $questionDetail->where('id', 0)->tap(function ($question) use ($questionDao) {
            $question->every(function ($detail) use ($questionDao) {
                $questionDao->QuestionDetail()->save(new QuestionDetail($detail));
                return true;
            });
        });

        $questionDao->save();

        return true;
    }

    public function del(int $id)
    {
        return $this->questionDao->where('id', $id)->delete();
    }


    public function setFieldStatus(int $id, string $field, int $value)
    {
        return $this->questionDao->setField($id, $field, $value);
    }

}